package com.zhijiaoyun.controller;

import com.zhijiaoyun.common.Constant;
import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.controller.base.BaseController;
import com.zhijiaoyun.log.SystemLog;
import com.zhijiaoyun.model.Lesson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author shisfish
 * @date 2017/12/25
 */
@Controller
@RequestMapping(value = "/lesson", produces = "application/json; charset=utf-8")
public class LessonController extends BaseController {

    /**
     * 新增课时
     *
     * @param courseId
     * @param name
     * @param title
     * @param content
     * @param videoUrl
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{courseId}/addLesson", method = RequestMethod.POST)
    @SystemLog(function = "addLesson", description = "添加课时")
    public Map<String, Object> addLesson(@PathVariable("courseId") Long courseId, String name, String title, String content, String videoUrl, HttpServletRequest request) {
        String createUser = String.valueOf(request.getAttribute(Constant.CREATE_USER));
        Lesson lesson = new Lesson(courseId, name, title, videoUrl, 1, content, createUser);
        return lessonService.addLesson(lesson);
    }

    /**
     * 修改课程信息
     *
     * @param courseId
     * @param lessonId
     * @param name
     * @param title
     * @param content
     * @param videoUrl
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{courseId}/{lessonId}/modifyLesson", method = RequestMethod.POST)
    @SystemLog(function = "modifyLesson", description = "修改课时信息")
    public Map<String, Object> modifyLesson(@PathVariable("courseId") Long courseId,
                                            @PathVariable("lessonId") Long lessonId, String name, String title,
                                            String content, String videoUrl, HttpServletRequest request) {
        String createUser = String.valueOf(request.getAttribute(Constant.CREATE_USER));
        Lesson lesson = new Lesson(courseId, name, title, videoUrl, 1, content, createUser);
        lesson.setLessonId(lessonId);
        return lessonService.modifyLesson(lesson);
    }

    /**
     * 某个课程下面的课时信息
     *
     * @param courseId
     * @param name
     * @param title
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{courseId}/lessonList")
    public Map<String, Object> lessonList(@PathVariable("courseId") long courseId, String name, String title) {
        return lessonService.listByParams(courseId, name, title);
    }

    /**
     * 课时详情
     *
     * @param lessonId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{lessonId}/lessonDetail")
    public Map<String, Object> lessonDetail(@PathVariable("lessonId") long lessonId) {
        return lessonService.getLessonById(lessonId);
    }

    /**
     * 删除课时
     *
     * @param courseId
     * @param lessonId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{courseId}/{lessonId}/deleteLesson", method = RequestMethod.POST)
    @SystemLog(function = "deleteLesson", description = "删除课时")
    public Map<String, Object> deleteLesson(@PathVariable("courseId") long courseId, @PathVariable("lessonId") long lessonId) {
        return lessonService.deleteCourse(courseId, lessonId);
    }

    /**
     * 课时的排序
     *
     * @param lessonId
     * @param sort
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sort/{lessonId}")
    @SystemLog(function = "sort", description = "课程排序")
    public Map<String, Object> sort(@PathVariable long lessonId, int sort, HttpServletRequest request) {
        if (sort <= 0) {
            return responseErrorMap(DsConstant.CODE_COURSE_DELETE_ERROR_1, "请填写正确的数字");
        }
        String createUser = String.valueOf(request.getAttribute(Constant.CREATE_USER));
        return lessonService.sort(lessonId, sort, createUser);
    }

}
